define(function(require, exports, module) {

    'use strict';

    var LazyStream = require('plugins/lazystream');
    require('plugins/imgLoading');

    var Tabs = require('./tabs-load'),
        tabs = new Tabs('#tabs', {
            activeIndex: getHashIndex() || getIndex() || 0,
            initialLoad: true,
            url: getUrl,
            init: loadTabContent,
            load: loadHandler,
            multiple: false
        });

    function getUrl(tab) {
        return tab.data('url');
    }

    function setTabListNum(tablist, num) {
        tablist = tablist || tabs.getTabList();
        tablist.each( function(index, tab) {
            var countEl,
                key = 'num' + (index - 1).toString(),
                value = num[key];

            if (typeof value !== 'undefined' && value !== '') {

                countEl = $(tab).find('.count');

                if (countEl.length > 0 && countEl.text() != value) {
                    countEl.text(value);
                }

            }
        });
    }

    function loadTabContent(opts) {
        $('.jImg').imgLoading();
        lazyMoreFn(opts);
    }

    function loadBridge(opts) {
        tabs.load(opts, loadHandler);
    }

    function loadHandler (opts, data) {
        loadTabContent(opts);
    }

    function getIndex() {
        var search = location.search;
        if ( search.indexOf('from=index') > 0 ) {
            return 1;
        }
    }

    function getHashIndex(){
        var hash = location.hash.substring(1);
        if(hash>0){
            return hash;
        }
    }

    function lazyMoreFn(opts){
        var lazyMore = new LazyStream($(opts.tabContent).find('.jPage'), {
            plUrl: $(opts.tab).attr('data-url'),
            paramFormater: function(n) {
                var data = {};
                data.pageNo = n;
                return data;
            },
            page:2,
            errorText: '<div class="loading">网络错误，点击重试</div>',
            loadingClass: 'loading',
            loadingText: '<div class="loading"><img src="../../image/plugins/loading.gif" class="load-gif" />正在加载，请稍后...</div>',
            load: function(el) {
                $(el).find('.jImg').imgLoading();
            },
            noAnyMore:'<div class="loading">sorry,已经没有下一页了...</div>'
        });
    }
});
